package com.cirnoworks.uhs.server;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.util.Arrays;

import com.cirnoworks.common.Base64;

public class UserKey implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 5788021936465389268L;
	private byte[] hash = new byte[32];

	public UserKey() {

	}

	public UserKey(byte[] hash) {
		if (hash.length != 32) {
			throw new RuntimeException("You must use a 256bit hash");
		}
		System.arraycopy(hash, 0, this.hash, 0, 32);
	}

	public UserKey(UserKey key) {
		hash = key.hash;
	}

	public byte[] getHash() {
		return hash;
	}

	public void pushHash(ByteBuffer buffer) {
		buffer.get(hash);
	}

	public void setHash(byte[] hash) {
		this.hash = hash;
	}

	public String getHashString() {
		return Base64.encode(hash);
	}

	public void setHashString(String hashString) {
		hash = Base64.decode(hashString);
	}

	@Override
	public int hashCode() {
		return Arrays.hashCode(hash);
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (getClass() != obj.getClass()) {
			return false;
		}
		UserKey other = (UserKey) obj;
		if (!Arrays.equals(hash, other.hash)) {
			return false;
		}
		return true;
	}

}
